/* tcpserver.c - tcpserver */

#include "tcpfms.h"
#include "tcb.h"
#include "port.h"

/*------------------------------------------------------------------------
 *  tcpserver - do a TCP passive open
 *------------------------------------------------------------------------
 */
int tcpserver(struct tcb *ptcb, u_short lport)
{
    if (lport == 0) {
        ptcb->tcb_state = TCPS_FREE;
        sem_destroy(&(ptcb->tcb_mutex));
        return SYSERR;
    }

    int nport = newport(lport, tcps_lqsize);
    if(nport < 0)
        return SYSERR;
    print_port(nport);

    ptcb->tcb_type = TCPT_SERVER;
    ptcb->tcb_lport = lport;
    ptcb->tcb_state = TCPS_LISTEN;
    ptcb->tcb_lqsize = tcps_lqsize;
    ptcb->tcb_listenq = nport;

    ptcb->tcb_smss = 0;
    sem_post(&(ptcb->tcb_mutex));
    return ptcb->tcb_dvnum;
}
